package com.hks360.car_treasure.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.socket.CommunicationManager;
import com.hks360.library.util.CommonUtil;

/**
 * Created by rwq on 2016/4/26.
 */
public class SevenClickView extends View implements View.OnClickListener{
    private Paint paint;
    private Bitmap frame;
    private Bitmap cirle;
    private Bitmap warm;
    private Bitmap cool;
//    private Bitmap stateOff;
//    private Bitmap stateOn;
    private float screenWidth;
    private float dx;
    private float dy = 30;
    private Display d;
    private boolean changeSuccess = false;

    public void setChangeSuccess(boolean changeSuccess) {
        this.changeSuccess = changeSuccess;
    }

    public void setCommunicationManager(CommunicationManager communicationManager) {
        this.communicationManager = communicationManager;
    }

    private CommunicationManager communicationManager = null;

    public void setIsClick(boolean isClick) {
        this.isClick = isClick;
    }

    private boolean isClick = false;

    public void setFlag(int flag) {
        this.flag = flag;
    }

    private int flag;
    private float  clickx;

    public void setListener(onFlagChangeListener listener) {
        this.listener = listener;
    }

    private onFlagChangeListener listener;
    public SevenClickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    public SevenClickView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        d = ((Activity) context).getWindowManager().getDefaultDisplay();
        screenWidth = d.getWidth();
        paint = new Paint();
        paint.setAntiAlias(true);
        frame = BitmapFactory.decodeResource(getResources(),R.drawable.control_frame);
        cirle = BitmapFactory.decodeResource(getResources(), R.drawable.control_circle);
        warm = BitmapFactory.decodeResource(getResources(),R.drawable.control_warm);
        cool = BitmapFactory.decodeResource(getResources(),R.drawable.control_cool);
//        stateOff = BitmapFactory.decodeResource(getResources(),R.drawable.state_off);
//        stateOn = BitmapFactory.decodeResource(getResources(),R.drawable.state_on);
        dx = (screenWidth - frame.getWidth()) / 2;
        setOnClickListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension((int)screenWidth,(int)(dy + frame.getHeight() + 40 ));
    }



    @Override
    protected void onDraw(Canvas canvas) {
        switch (flag){
            case -3:
                canvas.drawBitmap(cirle,dx, dy - 5,paint);
                break;
            case -2:
                canvas.drawBitmap(cirle,  (dx + ((dx + frame.getWidth() / 2 - 12.5f) - dx)/3 + 12.5f - cirle.getWidth()/2),dy - 5,paint);
                break;
            case -1:
                canvas.drawBitmap(cirle,dx + ((dx + frame.getWidth() / 2 - 12.5f) - dx) * 2/3 + 12.5f - cirle.getWidth()/2 ,dy - 5,paint);
                break;
            case 0:
                canvas.drawBitmap(cirle,dx + frame.getWidth() / 2  - cirle.getWidth()/2,dy - 5,paint);
                break;
            case 1:
                canvas.drawBitmap(cirle,(dx + frame.getWidth() / 2 - 12.5f) + ((dx + frame.getWidth() - 25) - (dx + frame.getWidth() / 2 - 12.5f)) / 3 + 12.5f - cirle.getWidth()/2,dy - 5,paint);
                break;
            case 2:
                canvas.drawBitmap(cirle,(dx + frame.getWidth() / 2 - 12.5f) + ((dx + frame.getWidth() - 25) - (dx + frame.getWidth() / 2 - 12.5f)) * 2/ 3 + 12.5f -cirle.getWidth()/2,dy - 5,paint);
                break;
            case 3:
                canvas.drawBitmap(cirle,dx + frame.getWidth() - cirle.getWidth() + 3,dy - 5,paint);
                break;
        }
        canvas.drawBitmap(frame, dx, dy, paint);
        canvas.drawBitmap(cool, dx / 2, dy, paint);
        canvas.drawBitmap(warm, dx * 5 / 4 + frame.getWidth(), dy, paint);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextSize(25);
        canvas.drawText("冷风", dx / 2, (float) ((dy * 2.2) + frame.getHeight()), paint);
        canvas.drawText("暖风", dx * 5 / 4 + frame.getWidth(), (float) ((dy * 2.2) + frame.getHeight()), paint);
        canvas.drawText("高", dx, dy + frame.getHeight() + 30, paint);
//        canvas.drawBitmap(stateOff, dx, dy + frame.getHeight() + 30, paint);
        canvas.drawText("中", dx + ((dx + frame.getWidth() / 2 - 12.5f) - dx)/3,dy+frame.getHeight()+30,paint);
        canvas.drawText("低",dx + ((dx + frame.getWidth() / 2 - 12.5f) - dx) * 2/3,dy+frame.getHeight()+30,paint);
        canvas.drawText("关",  (dx + frame.getWidth() / 2 - 12.5f),dy+frame.getHeight()+30,paint);
        canvas.drawText("低", (dx + frame.getWidth() / 2 - 12.5f) + ((dx + frame.getWidth() - 25) - (dx + frame.getWidth() / 2 - 12.5f)) / 3, dy + frame.getHeight() + 30, paint);
        canvas.drawText("中", (dx + frame.getWidth() / 2 - 12.5f) + ((dx + frame.getWidth() - 25) - (dx + frame.getWidth() / 2 - 12.5f)) * 2/ 3, dy + frame.getHeight() + 30, paint);
        canvas.drawText("高", dx + frame.getWidth() - 25, dy + frame.getHeight() + 30, paint);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                clickx = event.getX();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onClick(View view) {
        if(communicationManager.isFalse){
            return ;
        }
        if(isClick){
            if(clickx > dx - 10 && clickx <= dx + frame.getWidth()/14 + 20){
                flag = -3;
            }else if(clickx > dx + frame.getWidth()/14 + 20 && clickx <= dx + frame.getWidth()*3/14 + 20){
                flag = -2;
            }else if(clickx > dx + frame.getWidth()*3/14 + 20 && clickx <= dx + frame.getWidth()*5/14 + 20){
                flag = -1;
            }else if(clickx > dx + frame.getWidth()*5/14 + 20 && clickx <= dx + frame.getWidth()*7/14 + 20){
                flag = 0;
            }else if(clickx > dx + frame.getWidth()*7/14 + 20 && clickx <= dx + frame.getWidth()*9/14 + 20){
                flag = 1;
            }else if(clickx > dx + frame.getWidth()*9/14 + 20 && clickx <= dx + frame.getWidth()*11/14 + 20){
                flag = 2;
            }else if(clickx > dx + frame.getWidth()*11/14 + 20 && clickx <= dx + frame.getWidth() + 10){
                flag = 3;
            }
            listener.onChange(flag);
        }else {
            CommonUtil.showToast(getContext(),"车辆未启动!");
        }
        if(changeSuccess){
            invalidate();
            changeSuccess = false;
        }
    }
     public interface onFlagChangeListener{
        void onChange(int flag);
    }
}

